import { useUserStore } from '@/stores/user'

export function setupRouterGuards(router: any) {
  router.beforeEach((to: any, from: any, next: any) => {
    const userStore = useUserStore()
    
    // 页面刷新后检查登录状态
    if (!userStore.isLoggedIn) {
      userStore.checkLoginStatus()
    }
    
    if (to.path === '/login') {
      if (userStore.isLoggedIn) {
        next('/')
      } else {
        next()
      }
      return
    }
    
    if (!userStore.isLoggedIn) {
      next('/login')
    } else {
      next()
    }
  })
}